package org.wwb.antbox.user.web.config;

import org.ant.boot.starter.dubbo.base.config.AntDubboBaseAutoConfiguration;
import org.ant.boot.starter.dubbo.base.properties.DubboApplicationProperties;
import org.ant.boot.starter.dubbo.provider.config.AntDubboProviderAutoConfiguration;
import org.ant.boot.starter.dubbo.provider.properties.DubboProtocolProperties;
import org.ant.boot.starter.dubbo.provider.properties.DubboProviderProperties;
import org.ant.commons.commons.log.Log;
import org.ant.commons.commons.log.LogFactory;
import org.apache.dubbo.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author Bond
 * @Date: 2020/3/3 17:54
 * @Description: User dubbo 服务提供者配置
 */
@Configuration
@EnableConfigurationProperties({
        DubboProviderProperties.class,
        DubboApplicationProperties.class,
        DubboProtocolProperties.class})
@AutoConfigureAfter({
        AntDubboBaseAutoConfiguration.class,
        AntDubboProviderAutoConfiguration.class})
public class UserDubboProviderAutoConfiguration {

    private static final Log log = LogFactory.getLog(UserDubboProviderAutoConfiguration.class);

    @Autowired
    private DubboProviderProperties dubboProviderProperties;

    @Autowired
    private ProtocolConfig protocolConfig;

    @Autowired
    private ApplicationConfig applicationConfig;

    /**
     * Dubbo 注册中心配置
     * @return RegistryConfig
     */
    @Bean(name = "registryConfigUser")
    @ConditionalOnMissingBean(name = "registryConfigUser")
    @ConfigurationProperties(prefix = "org.ant.boot.dubbo.registry-user")
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        return registryConfig;
    }

    /**
     * Dubbo 元数据中心配置
     * @return
     */
    @Bean(name = "metadataReportConfig")
    @ConditionalOnMissingBean(name = "metadataReportConfig")
    public MetadataReportConfig metadataReportConfig() {
        MetadataReportConfig metadataReportConfig = new MetadataReportConfig();
        metadataReportConfig.setAddress(registryConfig().getAddress());
        return metadataReportConfig;
    }

    /**
     * Dubbo 服务提供者缺省值配置
     * @return ProviderConfig
     */
    @Bean("dubboProviderUser")
    public ProviderConfig dubboProviderUser(){
        ProviderConfig providerConfig = new ProviderConfig();

        providerConfig.setId(dubboProviderProperties.getId());
        // 延迟注册服务时间(毫秒)- ，设为-1时，表示延迟到Spring容器初始化完成时暴露服务
        providerConfig.setDelay(dubboProviderProperties.getDelay());
        // 远程服务调用超时时间(毫秒)
        providerConfig.setTimeout(dubboProviderProperties.getTimeout());
        // 服务提供方远程调用过程拦截器名称，多个名称用逗号分隔
        providerConfig.setFilter(dubboProviderProperties.getFilter());
        // 服务版本
        providerConfig.setVersion(dubboProviderProperties.getVersion());
        // 远程服务调用重试次数
        providerConfig.setRetries(dubboProviderProperties.getRetries());

        providerConfig.setApplication(applicationConfig);
        providerConfig.setProtocol(protocolConfig);
        // 分组
        providerConfig.setGroup(registryConfig().getGroup());
        log.info("DubboProvider-User 配置的注册中心分组为:【{0}】",providerConfig.getGroup());
        return providerConfig;
    }
}
